package cn.basic.controller;

import cn.basic.bean.AsynclFutureService;
import cn.basic.bean.bo.CommonBo;
import cn.basic.bean.resp.*;
import cn.basic.bean.vo.RequestVo;
import cn.basic.enums.ReturnCodeEnum;
import cn.basic.exception.DONotAlarmException;
import cn.basic.service.IRecordService;
import cn.basic.service.batch.BatchPsbUserComponent;
import cn.basic.util.RespUtil;
import cn.basic.util.StringUtils;
import cn.basic.util.ValidationUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

/**
 * @description:Controller
 * @author: ZZZ
 * @time: 2021/2/24 13:36
 */
@RestController
public class RecordController {

    @Autowired
    private IRecordService service;


    @Autowired
    @Qualifier(value ="dataExecutor" )
    private Executor executor;

    @Autowired
    private AsynclFutureService asynclFutureService;

    @Autowired
    private BatchPsbUserComponent batchPsbUserComponent;
    @RequestMapping("/batch1")
    public Object batch(int n){
        Long result = 0L;
        for (int i = 0; i < 100; i++) {
            result = result+batchPsbUserComponent.batchPsbUserOpenIdByPartId(i);
        }
        return result;
    }
    @RequestMapping("/optime1")
    public Object batch(String optime){
        Long result = batchPsbUserComponent.batchPsbUserOpenIdByOpenTime(optime);
        return result;
    }

    @RequestMapping("/test")
    public String test(){
        Future<Long> test = asynclFutureService.test();
        Long n=0L;
        try {
            n=test.get();
            System.out.println("==========================="+test.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return "总数 "+ n;
    }

    /**
     * @param requestVo
     * @param bindingResult
     * @name: exitAndEntry
     * @description: 华侨备案上传、查询、下载
     * @return: cn.anicert.filing.bean.bo.CommonBo<?>
     * @auther: ZZZ
     * @date: 2021/2/25 16:38
     */
    @PostMapping("/exit-and-entry")
    public Object exitAndEntry(@RequestBody  @Validated RequestVo requestVo, BindingResult bindingResult) throws Exception {
        //参数校验
         ValidationUtil.validate(bindingResult);
        //华侨备案上报 HQ000ZZYY001  华侨备案状态查询 HQ000ZZYY002  华侨备案下载 HQ000ZZYY003
        //app_saveApplyGazFtzLabel 网上续签核查入库
        CommonBo<?> commonBo=null;
        switch (requestVo.getServiceCode()) {
            case "HQ000ZZYY001": {
                commonBo=report(requestVo);
                break;
            }
            case "HQ000ZZYY002": {
                commonBo=queryStatus(requestVo);
                break;
            }
            case "HQ000ZZYY003": {
                commonBo=download(requestVo);
                break;
            }
            case "app_saveApplyGazFtzLabel":{
                commonBo=appSaveApplyGazFtzLabel(requestVo);
                break;
            }
            case "Z034D232SSD1":{
                String result= "{\"callSn\":\"APP000001202101301830208532751\",\"code\":103,\"message\":\"查询无结果\",\"responseTime\":\"20210130183021468\",\"result\":null,\"secretKey\":\"\"}";
                JSONObject jsonObject = JSONObject.parseObject(result);
                return jsonObject;
                //break;
            }
            case "Z03Y00F12BHA":{
                String result= "{\"callSn\":\"APP000001202101301828279092739\",\"code\":0,\"message\":\"请求成功\",\"responseTime\":\"20210130182828808\",\"result\":{\"birthDate\":\"19970109\",\"chnName\":\"温蔓娟\",\"engName\":\"\",\"idDetails\":[{\"changeNumber\":0,\"dateOfIssue\":\"20190307\",\"expireDate\":\"20290306\",\"idNumber\":\"CA7369344\",\"idType\":\"21\",\"isExpired\":false,\"isLost\":\"00\",\"lostDate\":\"\",\"placeOfIssue\":\"4415\",\"placeOfIssueName\":\"广东汕尾市\",\"version\":\"\",\"visaValidNumberAppResult\":null}]},\"secretKey\":\"t1p4x292\"}";
                JSONObject jsonObject = JSONObject.parseObject(result);
                return jsonObject;
                //break;
            }
            default: {
                commonBo = RespUtil.commonResp(ReturnCodeEnum.REQUEST_SUCCESS.getCode(), ReturnCodeEnum.REQUEST_SUCCESS.getMessage(), requestVo.getCallSn(), requestVo.getSecretKey(), null);
            }
        }
        return commonBo;
    }

    public CommonBo<?> report(RequestVo requestVo) throws Exception {
        ReportResultData resultData = service.report(requestVo.getRequestParams());
        return RespUtil.commonResp(ReturnCodeEnum.REQUEST_SUCCESS.getCode(), ReturnCodeEnum.REQUEST_SUCCESS.getMessage(), requestVo.getCallSn(), requestVo.getSecretKey(), resultData);

    }

    public CommonBo<?> queryStatus(RequestVo requestVo) throws Exception {
        if (StringUtils.isBlank(requestVo.getRequestParams().getBusinessNumber())) {
            throw new DONotAlarmException(ReturnCodeEnum.PARAMETER_ERROR.getCode(), "businessNumber不能为空");
        }
        QueryStatusResultData resultData = service.queryStatus(requestVo.getRequestParams());
//        if (resultData == null) {
//            throw new DONotAlarmException(ReturnCodeEnum.QUERY_NO_RESULT.getCode(), ReturnCodeEnum.QUERY_NO_RESULT.getMessage());
//        }
        return RespUtil.commonResp(ReturnCodeEnum.REQUEST_SUCCESS.getCode(), ReturnCodeEnum.REQUEST_SUCCESS.getMessage(), requestVo.getCallSn(), requestVo.getSecretKey(), resultData);
    }


    public CommonBo<?> download(RequestVo requestVo) throws Exception {
        if (StringUtils.isBlank(requestVo.getRequestParams().getBusinessNumber())) {
            throw new DONotAlarmException(ReturnCodeEnum.PARAMETER_ERROR.getCode(), "businessNumber不能为空");
        }
        DownloadResultData resultData = service.download(requestVo.getRequestParams());
//        if (resultData == null) {
//            throw new DONotAlarmException(ReturnCodeEnum.QUERY_NO_RESULT.getCode(), ReturnCodeEnum.QUERY_NO_RESULT.getMessage());
//        }
        return RespUtil.commonResp(ReturnCodeEnum.REQUEST_SUCCESS.getCode(), ReturnCodeEnum.REQUEST_SUCCESS.getMessage(), requestVo.getCallSn(), requestVo.getSecretKey(), resultData);
    }

    public CommonBo<?> appSaveApplyGazFtzLabel(RequestVo requestVo) throws Exception {
        Sfmxb sfmxb=new Sfmxb();
        List<Sfmxb> sfmxbList=new ArrayList<>();
        sfmxbList.add(sfmxb);

        String ywbh=requestVo.getRequestParams().getInfo().getYwbh();
        String sqlb	=requestVo.getRequestParams().getInfo().getSqlb();

        appSaveApplyGazFtzLabelResultData resultData=new appSaveApplyGazFtzLabelResultData(ywbh,sqlb,sfmxbList);
        return RespUtil.commonResp(ReturnCodeEnum.REQUEST_SUCCESS.getCode(), ReturnCodeEnum.REQUEST_SUCCESS.getMessage(), requestVo.getCallSn(), requestVo.getSecretKey(), resultData);
    }

}
